package v1

import (
	"github.com/gin-gonic/gin"
	"manager/auth"
	"manager/bll"
	"manager/errors"
	"manager/model"
	"manager/server/web/middleware"
	"manager/utils"
)

var CyclingStatics = &cyclingStatics{}

func init() {
	// 注册路由
	RegisterRouter(CyclingStatics)
}

type cyclingStatics struct{}

// Init 初始化路由
func (a *cyclingStatics) Init(r *gin.RouterGroup) {
	// web 路由
	g := r.Group("/cyclingStatics", middleware.Auth())
	{
		g.POST("/track_statics", a.trackStatics)
	}
	// app 路由
	appG := r.Group("app/cyclingStatics", middleware.Auth())
	{
		appG.POST("/track_statics", a.trackStatics)
	}
}

// trackStatics 统计
func (a *cyclingStatics) trackStatics(c *gin.Context) {
	var (
		in  = &model.CyclingStaticsReq{}
		out = &model.CyclingStaticsRsp{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	// 车辆校验
	if auth.ContextOs(c.Request.Context()) != "web" {
		if in.EbikeId == 0 {
			c.Error(errors.New("ebikeId is required"))
			return
		}
	}

	// 获取用户骑行数据
	if out, err = bll.CyclingStatics.GetUserCyclingData(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}
